package site.qjdb.controller;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import site.qjdb.constant.Info;
import site.qjdb.pojo.Notice;
import site.qjdb.pojo.User;
import site.qjdb.pojo.response.CommonResult;
import site.qjdb.pojo.response.ResponseCode;
import site.qjdb.service.NoticeService;
import site.qjdb.service.UserService;
import site.qjdb.util.CommonUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;


@Controller
@RequestMapping("/notice")
public class NoticeController {
	@Resource
	private NoticeService noticeService;
	@Resource
	private UserService userService;
	
	/**
	 * 	查询用户所有新通知
	 * 
	 * */
	@ResponseBody
	@RequestMapping("/queryAll")
	public Object queryAll(HttpServletRequest request,
			HttpSession session,
			HttpServletResponse response) {
		// 处理session和Cookie过期问题
		int i = CommonUtils.handleSCExpire(request.getCookies(), session, response, userService);
		if(i == 0) {
			return new CommonResult<>(ResponseCode.UNLOGIN);
		}
		return new CommonResult<>(ResponseCode.SUCCESS, noticeService.findNoticeAll(((User)session.getAttribute(Info.USER_SESSION)).getUid()));
	}
	
	/**
	 * 	根据类型查询通知
	 * 	type: 通知的类型，必传
	 * 
	 * */
	@ResponseBody
	@RequestMapping("/queryByType")
	public Object queryByType(@RequestParam String type,
			HttpServletRequest request,
			HttpSession session,
			HttpServletResponse response) {
		// 处理session和Cookie过期问题
		int i = CommonUtils.handleSCExpire(request.getCookies(), session, response, userService);
		if(i == 0) {
			return new CommonResult<>(ResponseCode.UNLOGIN);
		}
		List<String> list = Arrays.asList(new String[] {
				"1",
				"2",
				"3"
		});
		if(!list.contains(type)) {
			return new CommonResult<>(ResponseCode.ILLEGAL_NUMBER);
		}
		Integer uid = ((User)session.getAttribute(Info.USER_SESSION)).getUid();
		Integer typeInteger = Integer.parseInt(type);
		List<Notice> noticeList = noticeService.findNoticeByType(uid, typeInteger);
		// 清除新通知标记
		noticeService.clearNew(uid, typeInteger);
		return new CommonResult<>(ResponseCode.SUCCESS, noticeList);
	}
	
	/**
	 * 	删除通知
	 *	id: 通知nid，必传
	 * 
	 * 
	 * */
	@ResponseBody
	@RequestMapping("/delete")
	public Object delete(@RequestParam("id") String nid,
			HttpServletRequest request,
			HttpSession session,
			HttpServletResponse response) {
		// 处理session和Cookie过期问题
		int i = CommonUtils.handleSCExpire(request.getCookies(), session, response, userService);
		if(i == 0) {
			return new CommonResult<>(ResponseCode.UNLOGIN);
		}
		try {
			Notice notice = new Notice();
			notice.setNid(Integer.parseInt(nid));
			noticeService.deleteNotice(notice);
			return new CommonResult<>(ResponseCode.SUCCESS);
		}catch(Exception e) {
			return new CommonResult<>(ResponseCode.ILLEGAL_NUMBER);
		}
	}
}
